#include "n2n.h"
#include "ui_n2n.h"
#include <QMessageBox>
#include <QInputDialog>
#include <QFile>
#include <QTextStream>
#include <QDir>
#include <QThread>
#include <QCryptographicHash>
#include <QByteArray>


N2N::N2N(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::N2N)
{
    ui->setupUi(this);
    QDir path(QDir::homePath() + "/.config/qn2n/lans/");
    QStringList files = path.entryList(QDir::Files);
    ui->comboBox->addItems(files);
    ui->comboBox->show();
}

N2N::~N2N()
{
    delete ui;
}

class MyThread: public QThread
{
 public:
  virtual void run()
  {
        system("gksu ./Conect");
  }
};

static bool zadani(QString server,QString port,QString sit,QString heslo,QString ip,QString maska)
{
    if (server == "")
    {
        QMessageBox::warning(0, QString::fromLocal8Bit("Chybí server!"), QString::fromLocal8Bit("Pole nesmí být prázdné!"));
        return false;
    }
    else
    {
        if (port == "")
        {
            QMessageBox::warning(0, QString::fromLocal8Bit("Chybí port serveru!"), QString::fromLocal8Bit("Pole nesmí být prázdné!"));
            return false;
        }
        else
        {
            if (sit == "")
            {
                QMessageBox::warning(0, QString::fromLocal8Bit("Chybí název sítě!"), QString::fromLocal8Bit("Pole nesmí být prázdné!"));
                return false;
            }
            else
            {
                if (heslo == "")
                {
                    QMessageBox::warning(0, QString::fromLocal8Bit("Chybí heslo!"), QString::fromLocal8Bit("Pole nesmí být prázdné!"));
                    return false;
                }
                else
                {
                    if (ip == "")
                    {
                        QMessageBox::warning(0, QString::fromLocal8Bit("Chybí IP adresa!"), QString::fromLocal8Bit("Pole nesmí být prázdné!"));
                        return false;
                    }
                    else
                    {
                        if (maska == "")
                        {
                            QMessageBox::warning(0, QString::fromLocal8Bit("Chybí maska sítě!"), QString::fromLocal8Bit("Pole nesmí být prázdné!"));
                            return false;
                        }
                        else
                        {
                            return true;
                        }
                    }
                }
            }
        }
    }
}

static QString conect(QString data)
{
    QFile filed("/tmp/start.dat");
         if (!filed.open(QIODevice::WriteOnly | QIODevice::Text))
             return 0;

         QTextStream out(&filed);
         out << data;
    return 0;
}
static QString odpoj()
{
    system("gksu ./disconect");
    return 0;
}



void N2N::on_pushButton_clicked()
{
    QString server = ui->lineEdit_1->text();
    QString port = ui->lineEdit_2->text();
    QString sit = ui->lineEdit_3->text();
    QString heslo = (ui->lineEdit_4->text());
    QString ip = ui->lineEdit_5->text();
    QString maska = ui->lineEdit_6->text();
    QString data = server + ";" + port +  ";" + sit + ";" + heslo + ";" + ip +";"+ maska;
    if(zadani(server,port,sit,heslo,ip,maska))
    {
        ui->comboBox->setEnabled(false);
        ui->lineEdit_1->setEnabled(false);
        ui->lineEdit_2->setEnabled(false);
        ui->lineEdit_3->setEnabled(false);
        ui->lineEdit_4->setEnabled(false);
        ui->lineEdit_5->setEnabled(false);
        ui->lineEdit_6->setEnabled(false);
        ui->pushButton->setEnabled(false);
        ui->pushButton_2->setEnabled(true);
        conect(data);
        MyThread *th;
        th = new MyThread();
        th->start();
    }
}

void N2N::on_actionKonec_triggered()
{
    exit(0);
}

void N2N::on_pushButton_2_clicked()
{
    odpoj();
    ui->comboBox->setEnabled(true);
    ui->lineEdit_1->setEnabled(true);
    ui->lineEdit_2->setEnabled(true);
    ui->lineEdit_3->setEnabled(true);
    ui->lineEdit_4->setEnabled(true);
    ui->lineEdit_5->setEnabled(true);
    ui->lineEdit_6->setEnabled(true);
    ui->pushButton->setEnabled(true);
    ui->pushButton_2->setEnabled(false);
}

void N2N::on_actionUlo_it_S_triggered()
{
    QString server = ui->lineEdit_1->text();
    QString port = ui->lineEdit_2->text();
    QString sit = ui->lineEdit_3->text();
    QString heslo = ui->lineEdit_4->text();
    QString ip = ui->lineEdit_5->text();
    QString maska = ui->lineEdit_6->text();
    if (zadani(server,port,sit,heslo,ip,maska))
    {
        QString data = server + ";" + port +  ";" + sit + ";" + heslo + ";" + ip +";"+ maska;
        QString nazev;
        for (int i = 0; i < 1; i--)
        {
            bool ok = true;
            nazev = QInputDialog::getText(this, QString::fromLocal8Bit("Název souboru"),QString::fromLocal8Bit("Zadejte název pro uložení:"),QLineEdit::Normal,QString::null, &ok);
            if(ok)
            {
               if (nazev == "")
               {
                   QMessageBox::warning(0, QString::fromLocal8Bit("Chyba!"), QString::fromLocal8Bit("Nezadali jste název!"));
               }
               else
               {
                   i=2;
               }
            }
            else
            {
                i=2;
            }
        }
        nazev = nazev + ".lan";
        QString home = QDir::homePath() + "/.config/qn2n/lans/";
        QDir path;
        if (!path.exists(home))
        {
            path.mkpath(home);
        }
        QFile file(home+nazev);
             if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
                 return;

             QTextStream out(&file);
             out << data;
             ui->comboBox->addItem(nazev);
             ui->comboBox->show();
    }
}


void N2N::on_comboBox_currentIndexChanged(const QString &arg1)
{

}

void N2N::on_comboBox_textChanged(const QString &arg1)
{

}

void N2N::on_comboBox_activated(const QString &arg1)
{
    ui->comboBox->setEnabled(false);
    ui->lineEdit_1->setEnabled(false);
    ui->lineEdit_2->setEnabled(false);
    ui->lineEdit_3->setEnabled(false);
    ui->lineEdit_4->setEnabled(false);
    ui->lineEdit_5->setEnabled(false);
    ui->lineEdit_6->setEnabled(false);
    ui->pushButton->setEnabled(false);
    ui->pushButton_2->setEnabled(true);
    QString data;
    QFile file(QDir::homePath() + "/.config/qn2n/lans/"+arg1);
         if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
         return;

         QTextStream in(&file);
         while (!in.atEnd())
         {
             data = in.readLine();
         }
         conect(data);
         MyThread *th;
         th = new MyThread();
         th->start();
}

void N2N::on_pushButton_pressed()
{

}
